package com.dikang.keer.coupon.service.impl;

import com.dikang.common.to.MemberPrice;
import com.dikang.common.to.SkuReductionTo;
import com.dikang.keer.coupon.entity.MemberPriceEntity;
import com.dikang.keer.coupon.entity.SkuLadderEntity;
import com.dikang.keer.coupon.service.MemberPriceService;
import com.dikang.keer.coupon.service.SkuLadderService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dikang.common.utils.PageUtils;
import com.dikang.common.utils.Query;

import com.dikang.keer.coupon.dao.SkuFullReductionDao;
import com.dikang.keer.coupon.entity.SkuFullReductionEntity;
import com.dikang.keer.coupon.service.SkuFullReductionService;
import org.springframework.transaction.annotation.Transactional;


@Service("skuFullReductionService")
public class SkuFullReductionServiceImpl extends ServiceImpl<SkuFullReductionDao, SkuFullReductionEntity> implements SkuFullReductionService {
    @Autowired
    private SkuLadderService skuLadderService;
    @Autowired
    private MemberPriceService memberPriceService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<SkuFullReductionEntity> page = this.page(
                new Query<SkuFullReductionEntity>().getPage(params),
                new QueryWrapper<SkuFullReductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional
    public void saveFullReduction(SkuReductionTo skuReductionTo) {
    // 6.4 sku的优惠，满减信息 keer_sms->sms_sku_ladder\sms_sku_full_reduction
    // 6.4.1sms_sku_ladder
     System.out.println("sms_sku_ladde"+skuReductionTo);
        SkuLadderEntity skuLadderEntity = new SkuLadderEntity();
        skuLadderEntity.setDiscount(skuReductionTo.getDiscount());
        skuLadderEntity.setFullCount(skuReductionTo.getFullCount());
        skuLadderEntity.setSkuId(skuReductionTo.getSkuId());
        skuLadderEntity.setAddOther(skuReductionTo.getCountStatus());
        skuLadderService.save(skuLadderEntity);
            // 6.4.2sms_sku_full_reduction
        SkuFullReductionEntity skuFullReductionEntity = new SkuFullReductionEntity();
        skuFullReductionEntity.setFullPrice(skuReductionTo.getFullPrice());
        skuFullReductionEntity.setReducePrice(skuReductionTo.getReducePrice());
        skuFullReductionEntity.setSkuId(skuReductionTo.getSkuId());
        skuFullReductionEntity.setAddOther(1);
        this.save(skuFullReductionEntity);
        // 6.4.3sms_member_price
        List<MemberPrice> memberPrices = skuReductionTo.getMemberPrices();
      if (memberPrices!=null&&memberPrices.size()>0){
          List<MemberPriceEntity> collect = memberPrices.stream().map((item) -> {
              MemberPriceEntity memberPriceEntity = new MemberPriceEntity();
              memberPriceEntity.setSkuId(skuReductionTo.getSkuId());
              memberPriceEntity.setMemberPrice(item.getPrice());
              memberPriceEntity.setMemberLevelId(item.getId());
              memberPriceEntity.setMemberLevelName(item.getName());
              memberPriceEntity.setAddOther(1);
              return memberPriceEntity;
          }).collect(Collectors.toList());
          memberPriceService.saveBatch(collect);
      }
    }

}